// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var footer = require("../footer/footer_gray");
	var top = require("../top/top_main");
	var constant = require("constant");
	var databind = require("databind");
	var html_account = require("./accountMain.html");
	var html_inputCash = require("./html_inputCash.html");
	var html_rebutReason = require("./html_rebutReason.html");
	var tools = require("tools");
	var dialog = require("dialog");
	var Eqianbao;
	var auditState;
	var authentication;
	var flag = false;
	var pageIndex = 1,
		pageSize = 8,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		$(".pages").html(html_account);
		footer.init();
		top.init();
		$("#head").removeClass("none");
		$("#footer").removeClass("none");
		router.init();
		getUserInfo();
		inquireCurrentEnterpriseDetail();
		inquireFinanceApplicationList();
		inquireEnterpriseFinanceSummaryPageList();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}
		//点击后退的时候高亮显示账户总览
		if(base.getData("sid") !=null){
			router.route("account/accountMain.html");
		}
		//点击头像  查看企业信息
		$(".headImg").unbind("click").click(function() {
			router.route("account/enterpriseDetail.html");
		});

		if ($("#headImg").attr("fileUuid") != "" && $("#headImg").attr("fileUuid") != null) {
			$("#headImgBig").attr("src", $("#headImg").attr("src"));
		}

		//添加超链接
		$(".goToApprove").attr("href", "../approve/approve_main.html");
		
		$(".goToEqianbao").unbind("click").click(function() {
			router.route("account/enterpriseDetail.html?Eqianbao=" + 1);
		});
		
		//企业资质认证修改
		$(".goToAuthentication").attr("href", "../approve/update_QualificationAuthentication.html");
		//企业帐户管理
		$(".goToAdministration").attr("href", "../approve/account_managements.html");
		
		
		//点击签章
		$(".mySignature").unbind("click").click(function() {
			mySignature();
		});

		$("#sbarAccountMain").unbind("click").click(function() {
			//			location.href="../account/account.html";
			router.route("account/accountMain.html");
		});

		$("#sbarAccountTrunk").unbind("click").click(function() {
			router.route("account/inquireLogisticsInfoMain.html");
		});
		
		$("#sbarAdministrationy").unbind("click").click(function() {
			router.route("account/adminiBusinessAccount.html");
		});
		//我的保证金
		$("#sbarAccountBail").unbind("click").click(function() {
			router.route("account/myBail.html");
		});

		$("#sbarAccountRecord").unbind("click").click(function() {
			router.route("account/accountRecord.html");
		});

		$("#sbarAccountLetter").unbind("click").click(function() {
			router.route("account/accountLetter.html");
		});

		$("#sbarAccountApply").unbind("click").click(function() {
			router.route("account/accountRecordApply.html");
		});
		
		$("#sbarAccountModify").unbind("click").click(function() {//修改认证记录
			router.route("account/accountRecordModify.html");
		});

		$("#myLoan").unbind("click").click(function() {
			router.route("account/myLoan.html");
		});

		$("#myBill").unbind("click").click(function() {
			router.route("account/myBill.html");
		});

		$("#myException").unbind("click").click(function() {
			router.route("exception/exceptionManage.html")
		});
		
		$("#myReplenish").unbind("click").click(function(){
			router.route("account/myReplenish.html");
		});
		
		//客户管理
		$("#supplierManage").unbind("click").click(function(){
			router.route("account/supplierManage.html");
		});
		
		//地址管理
		$("#addressManage").unbind("click").click(function(){
			router.route("account/addressManage.html");
		});
		
		//客户信息
		$("#clientInfoList").unbind("click").click(function(){
			router.route("account/clientInfo.html");
		});
		
		//客户订单
		$("#clientOrderList").unbind("click").click(function(){
			router.route("account/clientOrder.html");
		});
		
		//我的收益
		$("#myFinanceEarningList").unbind("click").click(function(){
			router.route("account/myFinanceEarning.html");
		});
		
		//我的账单
		$("#myFinanceBillList").unbind("click").click(function(){
			router.route("account/myFinanceBill.html");
		});
		
		//我的额度
		$("#myLinesList").unbind("click").click(function(){
			router.route("account/myLines.html");
		});
		
		//CRM管理
		$("#crmManage").unbind("click").click(function(){
			var userName = base.getData("userName");
			location.href="http://122.152.210.195:8099/crm/register?userName=" + userName;
		});
		
		//提货列表
		$("#warehousePickup").unbind("click").click(function(){
			router.route("account/warehousePickupList.html");
		});
		
		//指定供应商提货申请
		$("#createPickup1_add").unbind("click").click(function(){
			router.route("account/createPickup1.html");
		});
		//仓储类型提货列表-合同上传
		$("#createPickup1_changchuan").unbind("click").click(function(){
			router.route("account/warehousePickupList.html?pickupState=[3, 4, 7, 8]");
		});
		//仓储类型提货列表-合同签署
		$("#createPickup1_qianshu").unbind("click").click(function(){
			router.route("account/warehousePickupList.html?pickupState=5");
		});
		
		$(".nav_sub .leftMenuToggle").unbind("click").click(function(){
			$(this).parent().toggleClass("leftMenuClose");
		});
		
		$("#accountMain .searchBtn").unbind("click").click(function(){
			var waybillNumber = $("#accountMain input[name=waybillNumber]").val();
			var caseNumber = $("#accountMain input[name=caseNumber]").val();
			var financeNumber = $("#accountMain input[name=financeNumber]").val();
			var financeContractNumber = $("#accountMain input[name=financeContractNumber]").val();
			var financeContractSerialNumber = $("#accountMain input[name=financeContractSerialNumber]").val();
			inquireFinanceApplicationList(waybillNumber, caseNumber, financeNumber, financeContractNumber,financeContractSerialNumber);
		});
	};

	//获得用户信息
	var getUserInfo = function() {
		base.fnAjax({
			action: "inquireAccountDetailByAppSessionUuid.json",
			version: "v3.0",
			async: false,
			oData: {
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					$(".mobile").html(data.datas[0].fullName);
				} else {}
			}
		});
	};

	//获得用户信息
	var inquireCurrentEnterpriseDetail = function() {
		base.fnAjax({
			action: "inquireCurrentEnterpriseDetail.json",
			oData: {}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					auditState = data.datas[0].auditState;
					authentication = data.datas[0].authentication;
					
					/*if(authentication == 3) {//3:认证成功 
						$(".goToEqianbao").remove();
					}*/
					
					if (auditState == 0) {
						$(".hint").removeClass("none");
						$(".goToApprove").removeClass("none");
					} else if (auditState == 1 || auditState == 0){
						$(".hint").removeClass("none");
						if (data.datas[0].submitState) {
							$(".hint").html("您已提交资质认证，请耐心等待审核结果");
							$(".goToApprove").remove();
						} else {
							$(".hint").html("您已填写资质认证，可点击<a class='submit' href='javascript:void(0);'>这里</a>提交进入审核");
							$(".submit").unbind("click").click(function(){
//								dialog.showConfirmDialog("提交资质认证", "提交后不可修改,是否确定提交资质认证？", function(){
//									submitEnterprise();
//								});
								location.href="../account/account.html#account/enterpriseDetail";
							});
						}
					} else if (auditState == 2 || auditState == 4) {
						$(".hint").remove();
						$(".goToApprove").remove();
						//显示实名认证和我的签章
						$("#ulCSS").removeClass("none");
					} else if (auditState == 3) {
						$(".hint").removeClass("none");
						$(".hint").html("您提交的资质不通过，请重新认证");
						$(".goToApprove").removeClass("none");
					}
			
					if (data.datas[0].enterpriseType != 1) {
						$("#myClient").remove();
					}
				} else {
					$(".goToApprove").removeClass("none");
//					base.alert(2, data.message);
				}
			}
		});
	};

	//查询企业融资汇总信息
	var inquireEnterpriseFinanceSummaryPageList = function() {
		base.fnAjax({
			action: "inquireEnterpriseFinanceSummaryPageList.json",
			async: false,
			oData: {
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas.length != 0) {
						databind.fillForm($(".important_info"), data.datas[0]);
						$(".amount_should .span1").html(($(".data[name='totalLoanAmount']").html() * 1 - $(".data[name='totalRepayAmount']").html() * 1).toFixed(2));

						if ($(".amount_should .span1").html() < 0) {
							$(".amount_should .span1").html("0");
						}
						
						$(".amount_should .span1").html(tools.formatCurrency($(".amount_should .span1").html()));
						$(".data[name='totalLoanAmount']").html(tools.formatCurrency($(".data[name='totalLoanAmount']").html()));
						$(".data[name='totalRepayAmount']").html(tools.formatCurrency($(".data[name='totalRepayAmount']").html()));
						$(".data[name='financeAmount']").html(tools.formatCurrency($(".data[name='financeAmount']").html()));
					}
				} else {}
			}
		});
	};
	
	//提交企业认证
	var submitEnterprise = function() {
		base.fnAjax({
			action: "submitEnterprise.json",
			async: false,
			oData: {
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "提交成功!");
					location.href = "../account/account.html";
				} else {}
			}
		});
	};

	//企业查询融资信息列表
	var inquireFinanceApplicationList = function(waybillNumber, caseNumber, financeNumber, financeContractNumber,financeContractSerialNumber) {
		base.fnAjax({
			action: "inquireEnterpriseInProcessFinanceApplicationPageList.json",
			async: false,
			oData: {
				pageSize: 20,
				pageIndex: pageIndex,
				waybillNumber: waybillNumber,
				caseNumber: caseNumber,
				financeNumber:financeNumber,
				financeContractNumber:financeContractNumber,
				financeContractSerialNumber:financeContractSerialNumber
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#accountMainListTpl").html();
					var html = juicer(tpl, data);
					$("#accountMainListBody").html(html);
					$(".cancelApplication").unbind("click").click(function() {
						var financeId = $(this).parents("tr").attr("financeId");
						dialog.showConfirmDialog("取消融资", "确定要取消此笔融资申请？", function() {
							cancelFinanceApplication(financeId);
						});

					});

					$(".detailApplication").unbind("click").click(function() {
						var fi = $(this).parents("tr").attr("financeId");
						var fn = $(this).parents("tr").attr("financeNumber")
						router.route("account/accountDetail.html?financeId=" + fi + "&financeNumber=" + fn);
					});

					base.pageControl($("#main .pagerCon"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireFinanceApplicationList();
						});
				} else {}
			}
		});
	};

	//取消融资申请
	var cancelFinanceApplication = function(financeId) {
		base.fnAjax({
			action: "cancelFinanceApplication.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					//					location.href = "../account/account.html";
					router.route("account/accountMain.html");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};
	
	//点击签章
	var mySignature = function() {
		//判断是否已实名认证
		//1 未认证
		if(authentication != 3) { 
//			var ii = layer.open({
//				  title: '提示',
//				  type: 1,
//				  skin: 'layui-layer-rim', //加上边框
//				  area: ['250px', '150px'], //宽高
//				  content: html_rebutReason,
//				  btn: ['实名认证'],
//				  yes: function(){
//				  	layer.close(ii);
//				  	 router.route("account/enterpriseDetail.html?Eqianbao=" + 1);
//				  }
//				});
//				$("#reason").text("您还未完成实名认证，无法制章");
			base.alert(2, "您还未完成实名认证，无法制章");
			router.route("account/enterpriseDetail.html?Eqianbao=" + 1);
	} else {
		router.route("account/enterpriseDetail.html?mySignature=" + 1);
	}
//		//2 已认证
//		if(authentication == 3) {
//			
//		}
	};
	
	return {
		init: init
	};
});